$(function(){
    var orderBillList_datagrid;
    orderBillList_datagrid = $("#orderBillList_datagrid");

    orderBillList_datagrid.datagrid({
        url:'/orderBill/list',

        fitColumns:true,
        toolbar:'#datagrid_toolar',
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'insurancePolicyNumber',title:'保单号',width:60},
            {field:'orderBillNumber',title:'申请单号',width:60},
            {field:'organization',title:'机构部门',width:60,formatter:orgFormatter},
            {field:'product',title:'产品',width:100,formatter:productFormatter},
            {field:'client',title:'投保人姓名',width:100,formatter:applyClientFormatter},
            {field:'carNumber',title:'车牌号码',width:100},
            {field:'applyTime',title:'投保申请时间',width:100},
            {field:'beginTime',title:'保险起期',width:100},
            {field:'endTime',title:'保险止期',width:100},
            {field:'inputUser',title:'任务提交人',width:60,formatter:inputUserFormatter},
            {field:'state',title:'任务状态',width:60,formatter:stateFormatter}
        ]]
    });
    /*organizationId:organizationId,
     productId:productId,*/
    var cmdObj={
        //搜索
        orderBillList_search:function(){
            var organizationId=$("[name=organizationId]").val()
            var productId=$("[name=productId]").val()
            var orderBillNumber=$("[name=orderBillNumber]").val()
            var insurancePolicyNumber=$("[name=insurancePolicyNumber]").val()
            var realName=$("[name=realName]").val()
            var carNumber=$("[name=carNumber]").val()
            var engineNumber=$("[name=engineNumber]").val()
            var mainChassis=$("[name=mainChassis]").val()
            var inputUser=$("[name=inputUser]").val()
            var state=$("[name=state]").val()

            var beginTime_fullYear = new Date($("#beginTime").datebox("getValue")).getFullYear();
            var beginTime_month = new Date($("#beginTime").datebox("getValue")).getMonth();
            var beginTime_day = new Date($("#beginTime").datebox("getValue")).getDate();
            var beginTime_date = beginTime_fullYear +"/"+ (beginTime_month+1)+"/"+beginTime_day;

            var endTime_fullYear = new Date($("#endTime").datebox("getValue")).getFullYear();
            var endTime_month = new Date($("#endTime").datebox("getValue")).getMonth();
            var endTime_day = new Date($("#endTime").datebox("getValue")).getDate();
            var endTime_date = endTime_fullYear +"/"+ (endTime_month+1)+"/"+endTime_day;

            var applyTime_beginTime_fullYear = new Date($("#applyTime_beginTime").datebox("getValue")).getFullYear();
            var applyTime_beginTime_month = new Date($("#applyTime_beginTime").datebox("getValue")).getMonth();
            var applyTime_beginTime_day = new Date($("#applyTime_beginTime").datebox("getValue")).getDate();
            var applyTime_beginTime_date = applyTime_beginTime_fullYear +"/"+ (applyTime_beginTime_month+1)+"/"+applyTime_beginTime_day;

            var applyTime_endTime_fullYear = new Date($("#applyTime_endTime").datebox("getValue")).getFullYear();
            var applyTime_endTime_month = new Date($("#applyTime_endTime").datebox("getValue")).getMonth();
            var applyTime_endTime_day = new Date($("#applyTime_endTime").datebox("getValue")).getDate();
            var applyTime_endTime_date = applyTime_endTime_fullYear +"/"+ (applyTime_endTime_month+1)+"/"+applyTime_endTime_day;

            //var beginTime=$("[name=beginTime]").val()
            //var endTime=$("[name=endTime]").val()
            //var applyTime_beginTime=$("[name=applyTime_beginTime]").val()
            //var applyTime_endTime=$("[name=applyTime_endTime]").val()
            //console.log(beginTime_date!=NaN)
            //console.log(endTime_date!=NaN)
            //console.log(applyTime_beginTime_date!=NaN)
            //console.log(applyTime_endTime_date!=NaN)
            if(beginTime_date!=NaN||endTime_date!=NaN||applyTime_beginTime_date!=NaN||applyTime_endTime_date!=NaN) {
                orderBillList_datagrid.datagrid("load",{
                    organizationId:organizationId,
                    productId:productId,
                    orderBillNumber:orderBillNumber,
                    insurancePolicyNumber:insurancePolicyNumber,
                    applyTime_beginTime:applyTime_beginTime_date,
                    applyTime_endTime:applyTime_endTime_date,
                    realName:realName,
                    beginTime:beginTime_date,
                    endTime:endTime_date,
                    carNumber:carNumber,
                    engineNumber:engineNumber,
                    mainChassis:mainChassis,
                    inputUser:inputUser,
                    state:state

                })

            }else {
                $.messager.alert("温馨提示","请选择必填选项","info")
            }

        },
        //修改
        orderBillList_edit:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                console.log(selectedRow.id);
                window.location.href='/orderBill/orderBillEdit?orgId='+selectedRow.id
            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        },
        //删除
        carInfo_quit:function(){

        },
        //重新加载
        orderBillList_reload:function(){
            window.location.reload()
        },
        //暂存单页面的申请批改按钮
        orderBillList_checkApply:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                console.log(selectedRow);
                $("#dialog_checkApply").dialog("open")
                //window.location.href='/orderBill/orderBillEdit?orgId='+selectedRow.id
            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        },
        //申请续保按钮
        orderBillList_renewal:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                if(selectedRow.state=="12"){
                    $("#form_renewal").form("clear")
                    var productId = selectedRow.productId;
                    $.get("/product/selectOne?id="+productId,function(data){
                        $("#price").numberbox("setValue",data.paymentPrice)
                    },"json")
                    $("#dialog_renewal").dialog("open")
                }else {
                    $.messager.alert("温馨提示","选择的数据为无效保单,请重新选择","error")
                }
            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        },
        //保单明细
        orderBillList_tip:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                console.log(selectedRow)
                    $("#form_orderTips").form("clear")
                //客户信息
                selectedRow["realName"]=selectedRow.client.realName;
                selectedRow["idCardType"]=selectedRow.client.idCardType;
                selectedRow["idCardNumber"]=selectedRow.client.idCardNumber;
                //承保信息表单
                selectedRow["organizationId"]=selectedRow.organization.id;
                selectedRow["organizationName"]=selectedRow.organization.organizationName;
                selectedRow["productId"]=selectedRow.product.id;
                selectedRow["productName"]=selectedRow.product.name;
                selectedRow["rate"]=selectedRow.product.rate;
                selectedRow["basicPrice"]=selectedRow.product.basicPrice;
                selectedRow["paymentPrice"]=selectedRow.product.paymentPrice;
                selectedRow["coveragePrice"]=selectedRow.product.coveragePrice;
                //录入人
                selectedRow["inputUserName"]=selectedRow.inputUser.name;
                selectedRow["applyTime1"]=selectedRow.applyTime;
                $("#applyTime1").datebox('setValue','6/1/2012')
                //车库汽车信息
                selectedRow["sn"]=selectedRow.carInfo.sn;
                selectedRow["carType"]=selectedRow.carInfo.carType;
                selectedRow["basesNumber"]=selectedRow.carInfo.basesNumber;
                selectedRow["airRelease"]=selectedRow.carInfo.airRelease;
                selectedRow["seatNumber"]=selectedRow.carInfo.seatNumber;
                selectedRow["rateDuality"]=selectedRow.carInfo.rateDuality;
                selectedRow["mainChassis"]=selectedRow.carInfo.mainChassis;
                selectedRow["engineNumber"]=selectedRow.carInfo.engineNumber;
                selectedRow["newCarPrice"]=selectedRow.carInfo.newCarPrice;
                selectedRow["importation"]=selectedRow.carInfo.importation;
                selectedRow["power"]=selectedRow.carInfo.power;
                //客户车辆信息
                selectedRow["clientCarInfo.id"]=selectedRow.clientCarInfo.id;
                selectedRow["hasCarNumber"]=selectedRow.clientCarInfo.hasCarNumber;
                selectedRow["newCar"]=selectedRow.clientCarInfo.newCar;
                selectedRow["foreignCar"]=selectedRow.clientCarInfo.foreignCar;
                selectedRow["transferCar"]=selectedRow.clientCarInfo.transferCar;
                selectedRow["carNumberType"]=selectedRow.clientCarInfo.carNumberType;
                selectedRow["carNumber"]=selectedRow.clientCarInfo.carNumber;
                selectedRow["registerTime"]=selectedRow.clientCarInfo.registerTime;
                selectedRow["workingNature"]=selectedRow.clientCarInfo.workingNature;
                selectedRow["workingArea"]=selectedRow.clientCarInfo.workingArea;
                selectedRow["transferTime"]=selectedRow.clientCarInfo.transferTime;
                selectedRow["driverInfo"]=selectedRow.clientCarInfo.driverInfo;
                selectedRow["driverYears"]=selectedRow.clientCarInfo.driverYears;
                selectedRow["fleet"]=selectedRow.clientCarInfo.fleet;
                selectedRow["queryNumber"]=selectedRow.clientCarInfo.queryNumber;
                selectedRow["relationToClient"]=selectedRow.clientCarInfo.relationToClient;
                selectedRow["checkNumber"]=selectedRow.clientCarInfo.checkNumber;
                $("#form_orderTips").form("load",selectedRow)
                $("#dialog_tips").dialog("open")

            }else {
                $.messager.alert("温馨提示","请选择要修改的数据","info")
            }
        },
        renewalbtn_cancel:function(){
            $("#dialog_tips").dialog("close")
        },
        //任务痕迹
        orderBillList_sum:function(){
            var selectedRow = orderBillList_datagrid.datagrid("getSelected");
            if (selectedRow){
                $.get("/orderBill/selectDiclearInfo?id="+selectedRow.id,function(data){
                    console.log(data)
                    $("#form_diclearInfo").form("load",data)
                    $("#dialog_diclearInfo").dialog("open")
                },"json")
            }else {
                $.messager.alert("温馨提示","请选择要查看的数据","info")
            }
        }
    };
    $("#list_organization_id").combobox({
        onChange:function(){
            var id = $("#list_organization_id").combobox("getValue");

            $("#list_product_id").combobox({
                onBeforeLoad: function(param){
                    param.orgId = id;
                }
            })
            /*$("#product_id").combobox("reload")*/
        }
    })
    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    $("#dialog_checkApply").dialog({
        title: '批改申请说明',
        width: 330,
        height: 230,
        buttons:'#dialog_checkApplybtn',
        closed: true
    })
    $("#dialog_renewal").dialog({
        title: '续保',
        width: 450,
        height: 470,
        buttons:'#dialog_renewalbtn',
        closed: true
    })
    $("#dialog_tips").dialog({
        title: '保单详细',
        width: 1200,
        height: 730,
        buttons:'#dialog_orderBillTips',
        closed: true
    })
    $("#dialog_diclearInfo").dialog({
        title: '任务痕迹',
        width: 250,
        height: 390,
        closed: true
    });
    $("#share").numberbox({
        onChange:function(newValue,oldValue){
            var price = $("#price").numberbox("getValue");
            var numberPrice = parseFloat(price);
            var numberNewValue = parseFloat(newValue);
            var newPrice =  numberNewValue* numberPrice;
            console.log(newValue,oldValue)
            $("#sharePrice").numberbox("setValue",newPrice)
        }
    })
});
//*******************************************************
function applyClientFormatter(value,row,index){
    return value.realName;
}
function productFormatter(value,row,index){
    return value.name;
}
function orgFormatter(value,row,index){
    return value.organizationName;
}
function stateFormatter(value,row,index){
    if(value==0){
        return '暂存单'
    }else if(value==10){
        return '申请批改'
    }else if(value==11){
        return '批改通过'
    }else if(value==12){
        return '批改未通过'
    }else if(value==20){
        return '申请核保'
    }else if(value==21){
        return '核保通过'
    }else if(value==22){
        return '核保为通过'
    }else if(value==31){
        return '有效保单'
    }else if(value==32){
        return '无效保单'
    }else if(value==41){
        return '续保保单';
    }
    //return "<font color='blue'>续保保单</font>";

    //return "<font color='#FF4500'>批改未通过</font>";
}
function inputUserFormatter(value,row,index){
    if(value!=null){
        return value.name;
    }
}
//申请批改按钮
function check_apply(){
    $("#dialog_checkApply").dialog("open")

}

//信息填写完成确认
function checkApply_ok(){
    var text = $("#applyText").textbox("getText");
    var selectedRow = $("#orderBillList_datagrid").datagrid("getSelected");
    $.messager.confirm("温馨提示","是否确认提交批改",function(ok){
        if(ok){
            $.post("/orderBill/checkApply",{orderId:selectedRow.id,remark:text},function(data){
                if (data.success){
                    window.location.reload()
                    $("#dialog_checkApply").dialog("close")
                    $.messager.alert("温馨提示",data.msg,"info")
                }else {
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            },"json")
        }
    })

}
//信息填写取消
function checkApply_cancel(){
    $("#dialog_checkApply").dialog("close")
}
//提交续保信息
function renewalbtn_ok(){
    var selectedRow = $("#orderBillList_datagrid").datagrid("getSelected");
    var orderId = selectedRow.id;
    $("#form_renewal").form("submit",{
        queryParams:{orderId:orderId},
        url:'/renewal/saveRenewal',
        success:function(data){
            data = $.parseJSON(data);
            $("#dialog_renewal").dialog("close")
            $.messager.alert("温馨提示",data.msg,"info")
        }
    })
}
//提交续保取消
function renewalbtn_cancel(){
    $("#dialog_renewal").dialog("close")
}